﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;
using EmptyWallGallery.Core;

namespace EmptyWallGallery.FrontEnd.Controls
{
    public partial class ArticlePrint : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetArticle();
        }

        public void GetArticle()
        {
            try
            {
                var sArticleId = Request.QueryString["ArticleID"].Replace(".aspx", "").Replace(".php", "").Replace(".htm", "").Replace(".asp", "");
                var pageTitleRewrite = Request.QueryString["Title"];
                var pageUrl = Request.QueryString["Year"] + "/" + Request.QueryString["Month"] + "/" +
                              Request.QueryString["Day"] + "/" + pageTitleRewrite + "-" + sArticleId;

                var regex = new Regex("^[A-Fa-f0-9]*$");
                if (!regex.IsMatch(sArticleId))
                {
                    Response.Redirect("/Error?e=404", false);
                }

                var iArticleId = Core.Utilities.Common.Instance.IntValue(sArticleId);

                //get content article from xml
                var objArticle = Core.BLL.Articles.Instance.ReadXmlbyId(
                    iArticleId.ToString(CultureInfo.InvariantCulture),
                    Request.QueryString["Day"],
                    Request.QueryString["Month"],
                    Request.QueryString["Year"]);

                if (objArticle != null)
                {
                    if ("/" + pageUrl != objArticle.ArticlePath)
                    {
                        Response.Redirect("/Error?e=404", false);
                    }
                    else
                    {
                        if (!(objArticle.ReleaseDate > DateTime.Now))
                        {
                            //if (objArticle.PostVisible == 1)
                            //{
                            //    Response.Redirect("/Error?e=404", false);
                            //}
                            //else
                            //{

                            //}
                            LoadArticle(objArticle);
                        }
                        else
                        {
                            Response.Redirect("/Error?e=404", false);
                        }
                    }
                }
                else
                {
                    Response.Redirect("/Error?e=404", false);
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/Error?e=1", false);
                //Response.Write(ex.Message + "<br />");
                //Response.Write(ex.StackTrace);
            }
        }

        public void LoadArticle(Core.BLL.ArticleListView objArticle)
        {
            var pageTitle = objArticle.Title;
            PostInfo.InnerHtml = "Đăng vào: " +
                                 Convert.ToDateTime(objArticle.ReleaseDate).AddHours(Configs.TimeLine).ToString(
                                     "dd-MM-yyyy HH:mm tt");
            title.InnerHtml = "<a href=\"" + objArticle.ArticlePath + "\" title=\"" + objArticle.Title + "\">" +
                              objArticle.Title + "</a>";
            body.InnerHtml = Server.HtmlDecode(objArticle.Body);

            Page.Title = Server.HtmlDecode(pageTitle) + " - " + Configs.WebTitle;
            //Page.MetaDescription = Server.HtmlDecode(pageTitle) + " - " +
            //                       Core.Utilities.Common.Instance.FormatMultiline(objArticle.Abstract, true) + " - " +
            //                       Configs.WebDescription;
        }
    }
}